package ngat.oss;

import ngat.util.*;
import ngat.phase2.*;
import ngat.astrometry.*;

import java.io.*;
import java.text.*;

/** Holds static variables and accessors relating to telescope's current
 * status and long term settings.
 *
 * $Id: TelescopeParameters.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 *
 */
public class TelescopeParameters extends ConfigurationProperties {

    public static final long   DEFAULT_DOME_CLOSE_TIME = 30L;
    public static final long   DEFAULT_DOME_CLOSE_SAFETY_GAP = 200L;
    public static final double DEFAULT_DOME_LOW_LIMIT = 20;
    public static final double DEFAULT_DOME_ZENITH_EXCLUSION = 1.0;
    public static final double DEFAULT_SITE_LATITUDE = 0.0;
    public static final double DEFAULT_SITE_LONGITUDE = 0.0;
    public static final double DEFAULT_SITE_ALTITUDE = 0.0;
    public static final double DEFAULT_TELESCOPE_AZIMUTH_SLEW = 2.0;
    public static final double DEFAULT_TELESCOPE_ALTITUDE_SLEW = 2.0;
    public static final double DEFAULT_TELESCOPE_SLEW_ACCELERATION = 0.7;
    
    public TelescopeParameters() {
	super();	
    }

    /** -------------------------------------------
     * The closing time (msecs) for the dome.
     * -------------------------------------------*/
    private long domeCloseTime;
    
    public long getDomeCloseTime() { return domeCloseTime;}
    
    public void setDomeCloseTime(long in) { domeCloseTime = in;}

    /** -------------------------------------------
     * The RCS needs to start closing the dome prior to sunrise.
     * The operation must be complete at least this amount of
     * time before the sun appears above the horizon.
     * -------------------------------------------*/
    private long domeCloseSafetyGap;
    
    public long getDomeCloseSafetyGap() { return domeCloseSafetyGap;}
    
    public void setDomeCloseSafetyGap(long in) { domeCloseSafetyGap = in;}
    
    
    /** -------------------------------------------
     * The latitude of the site (rads).    
     * -------------------------------------------*/
    private double siteLatitude;
    
    public double getSiteLatitude() { return siteLatitude;}
    
    public void setSiteLatitude(double in) { siteLatitude = in;}
    
    /** -------------------------------------------
     * The longitude of the site (rads).    
     * -------------------------------------------*/
    private double siteLongitude;
    
    public double getSiteLongitude() { return siteLongitude;}
    
    public void setSiteLongitude(double in) { siteLongitude = in;}
    
    /** -------------------------------------------
     * The altitude of the site (metres).    
     * -------------------------------------------*/
    private double siteAltitude;
    
    public double getSiteAltitude() { return siteLongitude;}
    
    public void setSiteAltitude(double in) { siteLongitude = in;}

    /** -------------------------------------------
     * Radius of dome zenith exclusion zone.
     * -------------------------------------------*/
    private double domeZenithExclusion;

    public double getDomeZenithExclusion() { return domeZenithExclusion;}

    public void setDomeZenithExclusion(double in) { domeZenithExclusion = in;}

    /** -------------------------------------------
     * Low (elevation) limit of enclosure.
     * -------------------------------------------*/
    private double domeLowLimit;
    
    public double getDomeLowLimit() { return domeLowLimit;}
    
    public void setDomeLowLimit(double in)  { domeLowLimit = in;}

    /** -------------------------------------------
     * Telescope (AZ) slew rate (deg/sec).
     * -------------------------------------------*/
    private double telescopeAzimuthSlewRate;
    
    public double getTelescopeAzimuthSlewRate() { return telescopeAzimuthSlewRate;}
    
    public void setTelescopeAzimuthSlewRate(double in)  { telescopeAzimuthSlewRate = in;}

    /** -------------------------------------------
     * Telescope (ALT) slew rate (deg/sec).
     * -------------------------------------------*/
    private double telescopeAltitudeSlewRate;
    
    public double getTelescopeAltitudeSlewRate() { return telescopeAltitudeSlewRate;}
    
    public void setTelescopeAltitudeSlewRate(double in)  { telescopeAltitudeSlewRate = in;}


    /** -------------------------------------------
     * Telescope slew acceleration (deg/sec/sec).
     * -------------------------------------------*/
    private double telescopeSlewAcceleration;
    
    public double getTelescopeSlewAcceleration() { return telescopeSlewAcceleration;}
    
    public void setTelescopeSlewAcceleration(double in)  { telescopeSlewAcceleration = in;}



    public void configure(File file) throws IOException, IllegalArgumentException {
	load(new FileInputStream(file));

	domeCloseTime             = getLongValue("dome.close.time",
						 DEFAULT_DOME_CLOSE_TIME);	  
	domeCloseSafetyGap        = getLongValue("dome.close.safety.gap",
						 DEFAULT_DOME_CLOSE_SAFETY_GAP);	  
	siteLatitude              = Math.toRadians(getDoubleValue("site.latitude",
								  DEFAULT_SITE_LATITUDE));	   
	siteLongitude             = Math.toRadians(getDoubleValue("site.longitude",
								  DEFAULT_SITE_LONGITUDE));	  
	siteAltitude              = getDoubleValue("site.altitude",
						   DEFAULT_SITE_ALTITUDE);	  
	domeLowLimit              = Math.toRadians(getDoubleValue("dome.low.limit ",
								  DEFAULT_DOME_LOW_LIMIT));	  
	domeZenithExclusion       = Math.toRadians(getDoubleValue("dome.zenith.exclusion",
								  DEFAULT_DOME_ZENITH_EXCLUSION));	  
	telescopeAzimuthSlewRate  = Math.toRadians(getDoubleValue("telescope.azimuth.slew.rate",
								  DEFAULT_TELESCOPE_AZIMUTH_SLEW));
	telescopeAltitudeSlewRate = Math.toRadians(getDoubleValue("telescope.altitude.slew.rate",
								  DEFAULT_TELESCOPE_ALTITUDE_SLEW));		
	telescopeSlewAcceleration = Math.toRadians(getDoubleValue("telescope.slew.acceleration ",
								  DEFAULT_TELESCOPE_SLEW_ACCELERATION));
	
    }


    /** Get the time for a slew from Position 'start' to 'finish' in millis. */
    public float getSlewTime(Position start, Position finish) {
    // ## TEMP method to give approx value.

	double RA1  = (double)(start.getRA());
	double RA2  = (double)(finish.getRA());
	double dec1 = (double)(start.getDec());
	double dec2 = (double)(finish.getDec());
	
	double cpsi = Math.cos(dec1)*Math.cos(dec2)*Math.cos(RA1-RA2)+Math.sin(dec1)*Math.sin(dec2);
	return (float)(Math.abs(Math.acos(cpsi))/telescopeAzimuthSlewRate); 
	
	// better method .. but really need Telescope wrap position.
	// double h1 = (double)(start.getAltitude());
	// double h2 = (double)(finish.getAltitude());
	// double a1 = (double)(start.getAzimuth());
	// double a2 = (double)(finish.getAzimuth());
	// double s1 = Math.abs(h1-h2)/slewrate;
	// double s2 = Math.abs(a1-a2)/slewRate;
	// return Math.max(s1,s2);
    }

}


/** $Log: TelescopeParameters.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2006/11/27 08:37:05  snf
/** Initial revision
/**
/** Revision 1.4  2001/02/23 18:46:58  snf
/** Added stuff.
/**
/** Revision 1.3  2000/11/22 15:01:36  snf
/** Removed ref to OSS_Constants.
/**
/** Revision 1.2  2000/11/22 14:53:49  snf
/** Added import to ngat.util.
/**
/** Revision 1.1  2000/11/22 14:14:59  snf
/** Initial revision
/** */
